import com.demo.Student;
import com.demo.StudentDao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudent {
    @Test
    public void AddStudent() {
        //初始化
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-bean.xml");
        //获取对象
        StudentDao studentDao = (StudentDao) context.getBean("studentDao");
        //创建对象
        Student student = new Student();
        student.setId(5);
        student.setUsername("张三");
        student.setPassword("123456");
        student.setCourse("java");
        //添加学生，并返回结果
        int result = studentDao.addStudent(student);
        if (result > 0) {
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }

    }
    @Test
    public void DeleteStudent() {
        //初始化
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-bean.xml");
        //获取对象
        StudentDao studentDao = (StudentDao) context.getBean("studentDao");
        //创建对象
        Student student = new Student();
        student.setId(5);
        //删除学生，并返回结果
        int result = studentDao.deleteStudentById(5);
        if (result > 0) {
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }

    }
    @Test
    public void UpdateStudent() {
        //初始化
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-bean.xml");
        //获取对象
        StudentDao studentDao = (StudentDao) context.getBean("studentDao");
        //创建对象
        Student student = new Student();
        student.setId(5);
        student.setUsername("张三");
        student.setPassword("123456");
        student.setCourse("java");
        //更新学生，并返回结果
        int result = studentDao.updateStudentById(student);
        if (result > 0) {
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }

    }
    //按id修改密码
    @Test
    public void UpdatePassword() {
        //初始化
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-bean.xml");
        //获取对象
        StudentDao studentDao = (StudentDao) context.getBean("studentDao");
        //创建对象
        Student student = new Student();
        student.setId(5);
        student.setPassword("123456");
        //更新学生，并返回结果
        int result = studentDao.updatePasswordByIdt(5, Integer.parseInt("666666"));
        if (result > 0) {
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }

    }

}
